package com.luoying.itew.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.luoying.itew.R;
import com.luoying.itew.bean.MonitorBean;
import com.luoying.itew.databinding.ItemMonitorViewBinding;
import com.luoying.itew.util.StateContext;

import java.util.ArrayList;
import java.util.List;

public class MonitorAdapter extends RecyclerView.Adapter<MonitorAdapter.ViewHolder> implements Filterable {
    private List<MonitorBean> list = new ArrayList<>();
    private List<MonitorBean> filterList = new ArrayList<>();
    private MonitorOnItemClickListener listener;
    private Context context;

    public MonitorAdapter(MonitorOnItemClickListener listener, Context context) {
        this.listener = listener;
        this.context = context;
    }

    public void setList(List<MonitorBean> list) {
        if(list==null){
            list = new ArrayList<>();
        }
        this.list = list;
        this.filterList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMonitorViewBinding binding =  DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.item_monitor_view, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MonitorBean bean = filterList.get(position);
        if(bean!=null){
            holder.binding.setBean(bean);
            StateContext state ;
            if(bean.getMonitorPointId()==100000003){
                state = new StateContext(bean.getMonitorPointStatus(),0);
            } else {
                state = new StateContext(bean.getMonitorPointStatus(),bean.getWarnCoefficient());
            }
            holder.binding.monitorStateTv.setText(state.levelStr());
            holder.binding.monitorStateTv.setTextColor(context.getResources().getColor(state.levelColor()));
            holder.binding.executePendingBindings();
            holder.binding.setOnItemClickListener(listener);
        }
    }
    @Override
    public int getItemCount() {
        return filterList==null?0:filterList.size();
    }

    @Override
    public Filter getFilter() {
        return  new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String string = charSequence.toString();
                if(string.isEmpty()){
                    filterList = list;
                } else {
                    List<MonitorBean> tempList = new ArrayList<>();
                    for (MonitorBean monitorBean : list) {
                        //比较模糊比较
                        if(monitorBean.getMonitorPointName().contains(charSequence)
                                ||monitorBean.getMonitorArea().getFullName().contains(charSequence)){
                            tempList.add(monitorBean);
                        }
                    }
                    filterList = tempList;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = filterList;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                filterList = (List<MonitorBean>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ItemMonitorViewBinding binding;

        public ViewHolder(ItemMonitorViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public interface MonitorOnItemClickListener{
        void onItemShare(MonitorBean monitorBean);
        void onItemClick(MonitorBean monitorBean);
    }


}
